package com.code.module.permission.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.code.base.sys.pojo.SysMenu;
import com.code.base.sys.pojo.SysRole;
import com.code.base.sys.pojo.SysRoleMenu;
import com.code.base.sys.pojo.SysUserRole;
import com.code.base.sys.service.SysRoleMenuService;
import com.code.base.sys.service.SysUserRoleService;
import com.code.module.permission.persistence.PermissionMapper;

@Service
public class PermissionService {
	
	@Resource
	private PermissionMapper permissionMapper;
	
	@Resource
	private SysUserRoleService sysUserRoleService;
	
	@Resource
	private SysRoleMenuService sysRoleMenuService;
	
	public List<SysRole> userOwnRole(Long userId) {
		List<SysRole> list = permissionMapper.getRoleByUserId(userId);
		return list;
	}
	
	public List<SysMenu> userOwnMenu(Long userId) {
		List<SysMenu> list = permissionMapper.getMenuByUserId(userId);
		return list;
	}
	
	public void updateUserRole(Long userId, Long[] roleIds) {
		SysUserRole temp = new SysUserRole();
		temp.setSysUserId(userId);
		sysUserRoleService.deleteDataByObject(temp);
		
		for (Long roleId : roleIds) {
			SysUserRole m = new SysUserRole();
			m.setSysUserId(userId);
			m.setSysRoleId(roleId);
			
			sysUserRoleService.insertData(m);
		}
	}
	
	public List<SysMenu> roleOwnMenu(Long roleId) {
		List<SysMenu> list = permissionMapper.getMenuByRoleId(roleId);
		return list;
	} 
	
	public void updateRoleMenu(Long roleId, Long[] menuIds) {
		SysRoleMenu temp = new SysRoleMenu();
		temp.setSysRoleId(roleId);
		sysRoleMenuService.deleteDataByObject(temp);
		
		for (Long menuId : menuIds) {
			SysRoleMenu m = new SysRoleMenu();
			m.setSysRoleId(roleId);
			m.setSysMenuId(menuId);
			
			sysRoleMenuService.insertData(m);
		}
	}
}
